package com.cytech.cloudclinic.moudle.login;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import com.cytech.cloudclinic.AccountInfo;
import com.cytech.cloudclinic.MainActivity;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.dialog.ForgetPasswordDialog;
import com.yechaoa.yutils.SpUtil;

import butterknife.BindView;
import butterknife.OnClick;

import static com.cytech.cloudclinic.app.Constant.USER_TOKEN;

/**
 * 登录页面
 * 测试账号：13264616071
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginView{
    @BindView(R.id.et_login_account)
    EditText etAccount;
    @BindView(R.id.et_login_password)
    EditText etPassword;

    ForgetPasswordDialog forgetPasswordDialog;

    public static void actionStart(Context context){
        Intent intent = new Intent(context,LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.btn_login)
    public void login(View view){
        //MainActivity.actionStart(this);
        presenter.login(etAccount.getText().toString(),etPassword.getText().toString());
    }

    @OnClick(R.id.btn_login_forget_psw)
    public void forgetPsw(View view){
        if(forgetPasswordDialog == null){
            forgetPasswordDialog = new ForgetPasswordDialog();
        }
        forgetPasswordDialog.show(getSupportFragmentManager(),"password");
    }


    @Override
    public void loginSuccess(String token) {
        SpUtil.setString(USER_TOKEN,token);
        presenter.getUserInfo();

    }

    @Override
    public void getUserInfoSuccess(AccountInfo info) {
        SpUtil.setString(SpUtil.getString(USER_TOKEN),info.name);
        MainActivity.actionStart(this);
        finish();
    }
}
